﻿using UnityEngine;
using System.Collections;


public class Spawner : MonoBehaviour {
	
	//odnośnik do kontenera na postaci
	public GameObject characters;
	//licznik czasu do respawn'a
	private float timer;
	//aktualna ilość postaci na planszy
	public static int currentCharacterCounter;
	//maksymalna liczba postaci
	public static int maxCharacterCounter;
	//poziom paniki- do ustalania szybkości poruszania się postaci
	public static int panicLevel;
	private NextWaypoints nextWaypoints;
	// Use this for initialization
	void Start () {
		timer = 0.0f;
		currentCharacterCounter = 0;
		panicLevel = 0;
		nextWaypoints = GetComponent<NextWaypoints>();
		timer = Random.Range(0f,3f);
	}
	
	// Update is called once per frame
	void Update () {
		timer -= Time.deltaTime;
		//sprawdzenie czy można już stworzyć nową postać
		if (timer < 0f && currentCharacterCounter < maxCharacterCounter) {
			//losowanie: kobieta czy facet
			if (Random.Range(0, 2) == 0) {
				//instancjonowanie nowej postaci, w pozycji wylosowanego waypointa
				GameObject man = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Man/man"),
					transform.position,
					new Quaternion()) as GameObject;

				//przypisanie nowego prefaba do parenta (characters)
				man.transform.parent = characters.transform;
				CharacterFactory.attrib newAttrib = CharacterFactory.rollForAttributes(true);

				while(TargetSpawner.compareAttributes(newAttrib))
					newAttrib = CharacterFactory.rollForAttributes(true);

				//wybór twarzy postaci
				CharacterFactory.loadFace(true, man.transform, newAttrib.faceNmb);
				//wybór ubrania
				CharacterFactory.loadCloth(true, man.transform, newAttrib.clothNmb);
				//wybór włosów/czapki
				CharacterFactory.loadHead(true,
				                          man.transform,
				                          newAttrib.hairNmb,
				                          newAttrib.bandanaNmb,
				                          newAttrib.hoodNmb); 
				//dodanie komponentu sterującego postacią
				Waypoints newWaypoint = man.AddComponent<Waypoints>();
				newWaypoint.currentPoint = pickRandomWaypoint();
				newWaypoint.speed = Random.Range(0.15f,0.41f) + (float)panicLevel * 0.1f;
				//tworzona teraz postać NIE JEST celem!
				man.GetComponent<Trigger>().asTarget = false;
				currentCharacterCounter++;
			} else {
				//instancjonowanie nowej postaci, w pozycji wylosowanego waypointa
				GameObject woman = Instantiate(
					Resources.Load<GameObject>("Characters/Prefabs/Woman/woman"),
					transform.position,
					new Quaternion()) as GameObject;
				//przypisanie nowego prefaba do parenta (characters)
				woman.transform.parent = characters.transform;
				//stworzenie obiektu atrybutów postaci
				CharacterFactory.attrib newAttrib = CharacterFactory.rollForAttributes(false);
				while(TargetSpawner.compareAttributes(newAttrib))
					newAttrib = CharacterFactory.rollForAttributes(false);
				//wybór twarzy postaci
				CharacterFactory.loadFace(false, woman.transform, newAttrib.faceNmb);
				//wybór ubrania
				CharacterFactory.loadCloth(false, woman.transform, newAttrib.clothNmb);
				//wybór włosów/czapki
				CharacterFactory.loadHead(false,
				                          woman.transform,
				                          newAttrib.hairNmb,
				                          newAttrib.bandanaNmb,
				                          newAttrib.hoodNmb); 
				//dodanie komponentu sterującego postacią
				Waypoints newWaypoint = woman.AddComponent<Waypoints>();
				newWaypoint.currentPoint = pickRandomWaypoint();
				newWaypoint.speed = Random.Range(0.15f,0.41f) + (float)panicLevel * 0.1f;
				//tworzona teraz postać NIE JEST celem!
				woman.GetComponent<Trigger>().asTarget = false;
				currentCharacterCounter++;
			}
			timer = Random.Range(0f,3f);
		}
	}

	GameObject pickRandomWaypoint() 
	{
		int random = Random.Range(0,nextWaypoints.getNextWaypoints().Length);
		GameObject currentPoint = 
			nextWaypoints.getNextWaypoints() [random];
		return currentPoint;
	}
}
